package com.mldong.wxauth.controller;

import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.mldong.wxauth.base.CommonResult;
import com.mldong.wxauth.dto.LoginDTO;
import com.mldong.wxauth.entity.User;
import com.mldong.wxauth.service.UserService;
import com.mldong.wxauth.token.ITokenStore;
import com.mldong.wxauth.tool.AssertTool;
import com.mldong.wxauth.vo.TokenVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author mldong
 * @date 2022/1/4
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(value = "用户管理", tags = "用户管理")
public class UserController {
    private final ITokenStore tokenStore;
    private final UserService userService;
    @ApiImplicitParam(name = "id",value = "用户ID",required = true)
    @GetMapping("/get")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取用户详情", notes = "user:get")
    public CommonResult<User> get(String id) {
        return CommonResult.success(userService.getById(id));
    }
    @PostMapping("/login")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "用户登录", notes = "user:login")
    public CommonResult<TokenVO> login(@RequestBody LoginDTO dto) {
        if(StrUtil.isEmpty(dto.getUserName()) || StrUtil.isEmpty(dto.getPassword())) {
            AssertTool.throwBiz("用户名或密码不能为空");
        }
        return CommonResult.success(userService.login(dto));
    }
}
